package com.uml.umlwork.controller;


import com.uml.umlwork.config.annotation.PassToken;
import com.uml.umlwork.config.annotation.UserLoginToken;
import com.uml.umlwork.dto.user.UpdateUserDTO;
import com.uml.umlwork.dto.user.UserSignUpDTO;
import com.uml.umlwork.entity.User;
import com.uml.umlwork.service.UserService;
import com.uml.umlwork.utils.SystemMsgJsonResponse;
import com.uml.umlwork.vo.user.UserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Siki
 * @date 2020/12/21
 */
@Api(tags = "用户验证api")
@RequestMapping("/api/v1/umlwork/user")
@RestController
@CrossOrigin
public class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ApiOperation(value = "[统一登录接口]", httpMethod = "POST")
    public SystemMsgJsonResponse login(@RequestParam("username") String username,
                                       @RequestParam("password") String password) {
        return SystemMsgJsonResponse.success(userService.login(username, password));
    }

    @PostMapping("/signUp")
    @ApiOperation(value = "[注册接口]-学生/培训师", httpMethod = "POST")
    public SystemMsgJsonResponse signUp(@RequestBody @Validated UserSignUpDTO dto) {
        userService.signUp(dto);
        return SystemMsgJsonResponse.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "[统一修改密码接口]", httpMethod = "POST")
    @UserLoginToken
    public SystemMsgJsonResponse update(@RequestBody @Validated UpdateUserDTO dto,
                                        @RequestAttribute("userId") String id) {
        return userService.updatePassword(dto, id);
    }

    @PostMapping("/userInfo")
    @ApiOperation(value = "[用户信息获取]",httpMethod = "POST",response = UserInfoVO.class)
    @UserLoginToken
    public SystemMsgJsonResponse userInfo(@RequestAttribute("userId") String id){
        return SystemMsgJsonResponse.success(userService.getUserInfo(id));
    }

    @PostMapping("/logout")
    @ApiOperation(value = "[统一注销接口]",httpMethod = "POST")
    public SystemMsgJsonResponse logout(){
        return SystemMsgJsonResponse.success();
    }

}
